﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;

namespace MixNP.BusinessLayer
{
    public class BaseClass : System.Web.UI.Page
    {
        
        public bool RequiresLogin{ get; set; }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
        protected override void OnInit(EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.IsAuthenticated)
                {
                    if (Context.Session != null)
                    {
                        if (Context.Session.IsNewSession)
                        {
                            SetSession();
                        }
                    }
                    else
                    {
                        SetSession();
                    }

                    CheckProfile(User.Identity.Name);
                }
                else
                {
                    Session.Remove("UserId");

                    if (this.RequiresLogin)
                    {
                        RequestLoginPage();                    
                    }
                }
            }
            base.OnInit(e);
        }

        private void SetSession()
        {
            Session["UserId"] = User.Identity.Name;
        }

        private void CheckProfile(string emailAddress)
        {
            using (System.Data.DataTable table = MixNP.BusinessLayer.Profiles.GetProfile(emailAddress))
            {
                if (table.Rows.Count == 0)
                {
                    string currentUrl = HttpContext.Current.Request.RawUrl;
                    string profilePageUrl = "/account/profile.mix";

                    if (profilePageUrl != HttpContext.Current.Request.Path)
                    {
                        HttpContext.Current.Response.Redirect(String.Format("{0}?ReturnUrl={1}", profilePageUrl, currentUrl));
                    }
                }
            }
        }

        private void RequestLoginPage()
        {
            string currentUrl = HttpContext.Current.Request.RawUrl;
            string loginPageUrl = "/sign-in.mix";
            HttpContext.Current.Response.Redirect(String.Format("{0}?ReturnUrl={1}", loginPageUrl, currentUrl));
        }

    }
}
